from flask import jsonify,request, Blueprint
from configs import *
from modules.Tables import *
from sqlalchemy import and_
from flask_jwt_extended import create_access_token,get_jwt_identity,jwt_required

# 创建蓝图，对应的register目录
model_view = Blueprint('model_view', __name__)


# 模型应用创建接口
@model_view.route('/model_use_create', methods=['POST'])
def model_use_create():

    # 构建模型应用所需参数
    user_id = request.form.get('user_id')
    # 模型应用名称
    model_name = request.form.get('model_name')
    # 模型应用详情
    model_title = request.form.get('model_title')
    # 模型类型id
    t_model_type_id = request.form.get('t_model_type_id')

    # 去除所传数据中的首尾空格
    model_name = model_name.strip() if model_name else None
    model_title = model_title.strip() if model_title else None
    t_model_type_id = t_model_type_id.strip() if t_model_type_id else None


    if model_name is None or model_title is None or t_model_type_id is None:
        return jsonify({"msg": "模型应用数据必须填写",'code':400})

    # 查询user_id是否存在
    model_name_data = db.session.query(Model).filter(Model.model_name == model_name).first()
    if model_name_data:
            return jsonify({'code':400,'msg':'应用名称已经存在'})

    model_data = Model(model_name=model_name, model_title=model_title,t_model_type_id=t_model_type_id)
    db.session.add(model_data)
    db.session.commit()
    return jsonify({'code': 200, 'msg': '创建成功',})

# 模型应用数据删除接口
@model_view.route('/model_use_delete', methods=['POST','GET','DELETE'])
def model_use_delete():
    # 模型应用id
    model_id = request.form.get('model_id',None)
    # 去除所传数据中的首尾空格
    model_id = model_id.strip() if model_id else None

    if model_id is None :
        return jsonify({"msg": "模型应用数据必须选择",'code':400})

    model_res = db.session.query(Model).filter(Model.id == int(model_id)).first()
    if model_res:
        db.session.delete(model_res)  # 从数据库中删除记录
        db.session.commit()  # 提交事务以保存更改
        print("记录已删除")
    else:
        return jsonify({'code': 400, 'msg': '模型应用所要删除数据不存在', })

    return jsonify({'code': 200, 'msg': '删除成功', })


# 模型应用数据展示接口
@model_view.route('/model_use_show', methods=['POST','GET'])
def model_use_show():

    # user_id = request.args.get('user_id')

    # model_info = db.session.query(User, Model.model_name,Model.model_title, Model_Type.model_type_name).join(Model, User.id == Model.user_id).join(Model_Type, Model.t_model_type_id == Model_Type.id). \
    #     filter(and_(User.id == user_id, Model.user_id == user_id)).all()

    # 连表查询获取模型应用数据列表
    model_info = db.session.query(Model.model_name,Model.model_title, Model_Type.model_type_name).join(Model_Type, Model.t_model_type_id == Model_Type.id).all()
    model_list = [ {'model_name':i.model_name, 'model_title':i.model_title,'model_type_name':i.model_type_name} for i in model_info]

    return jsonify({'code': 200, 'msg': '查询成功','data':model_list})


# 模型类别 select 筛选返回
@model_view.route('/model_type_select', methods=['POST','GET'])
# @jwt_required() # token身份验证
def model_type_select():
    model_type = db.session.query(Model_Type).all()
    model_type_list = convert_folder_to_dict_list(model_type,['id','model_type_name','create_time'])
    return jsonify({'code': 200, 'msg': '查询成功','data':model_type_list})


# 模型数据集选择
@model_view.route('/new_data_versions', methods=['POST','GET'])
def new_data_versions():
    # 数据集文件id
    t_folder_id = request.form.get('t_folder_id')
    # 版本号
    version = request.form.get('version')
    # 训练集比例
    training_set = request.form.get('training_set')
    # 测试集比例
    test_set = request.form.get('test_set')
    # 设置为最新版本
    flag = request.form.get('flag')



# 模型配置接口
@model_view.route('/model_configs', methods=['POST','GET'])
def model_configs():
    pass